var axios = require('axios')
var config = require('./config')

var a_url = config.get('a_url')
var app_url = config.get('app_url')

//手机号 密码获取用户信息
function getToken(phone,pwd,callback){
    axios.get(a_url+'/cPasswordGetKey?username='+phone+'&password='+pwd+'&sertype=0')
        .then(res=>{
            if (res.data.code == 200) {
                axios.get(app_url + '/login?key='+res.data.data.key+'&sertype=0')
                .then(res=>{
                    if (res.data.code == 200) {
                        callback(false,res.data.data)
                    } else {
                        callback(true,res.data.data)
                    }
                })
            } else {
                callback(true,res.data.data)
            }
        })
}

//发送验证码
function sendCode(phone,callback){
    axios.get(a_url+'/cgetTelCodeToLogin?tel='+phone+'&sertype=0')
        .then(res=>{
            if (res.data.code == 200) {
                callback(false,"发送成功")
            } else {
                callback(true,res.data.data)
            }
        })
}

//手机号 验证码获取token
function codeGetToken(phone,code,callback){
    axios.get(a_url+'/cTelCodeLogin?tel='+phone+'&sertype=1&code='+code)
        .then(res=>{
            if (res.data.code == 200) {
                axios.get(app_url+'/index/index/login?key='+res.data.data.key)
                .then(res=>{
                    if (res.data.code == 200) {
                        callback(false,res.data.data)
                    } else {
                        callback(true,res.data.data)
                    }
                })
            } else {
                callback(true,res.data.data)
            }
        })
}

//切登录
function keyGetKey(key,type){
    // var urlList = ['http://deal.mingyukehui.com/home']
    var sertype = config.get('sertype')
    switch (type) {
        case 0:
            sertype == 0 ? '' :
            axios.get(a_url+'/checkoutLoginType?key='+key+'&sertype='+sertype)
            .then(res=>{
                console.log(res)
                // window.location = 'http://deal.mingyukehui.com/home?key='+res.data.data.key
            })
            break;
        case 1:
            sertype == 1 ? '' :
            axios.get(a_url+'/checkoutLoginType?key='+key+'&sertype='+sertype)
            .then(res=>{
                console.log(res)
                window.location = 'http://deal.mingyukehui.com/home?key='+res.data.data.key
            })
            break;
        case 2:
            sertype == 2 ? '' :
            axios.get(a_url+'/checkoutLoginType?key='+key+'&sertype='+sertype)
            .then(res=>{
                console.log(res)
                // window.location = 'http://deal.mingyukehui.com/home?key='+res.data.data.key
            })
            break;
        case 4:
            sertype == 4 ? '' :
            axios.get(a_url+'/checkoutLoginType?key='+key+'&sertype='+sertype)
            .then(res=>{
                console.log(res)
                // window.location = 'http://deal.mingyukehui.com/home?key='+res.data.data.key
            })
            break;
        default:
            break;
    }
    
}

//url带key 获取用户信息
function keyGetToken(key,type,callback){
    axios.get(a_url+'/checkoutLoginType?key='+key+'&sertype='+type)
        .then(res=>{
            if (res.data.code == 200) {
                axios.get(app_url+'/index/index/login?key='+res.data.data.key)
                .then(res=>{
                    if (res.data.code == 200) {
                        callback(false,res)
                    }
                })
            } else {
                callback(true,res.data.data)
            }
        })
}

//自动填充token		
function autoToken(){
    var token = config.get("userInfo").token
    axios.interceptors.request.use(config=>{
            config.url = config.url + "?token=" + token
            return config
        },function(err){

    })
}



//导出
module.exports = {
    getToken,
    sendCode,
    codeGetToken,
    keyGetToken,
    keyGetKey,
    autoToken
}